<?php

namespace app\controller\teacher;

use app\BaseController;
use app\Code;
use app\dao\MemberDao;
use app\validate\MemberValidate;
use think\exception\ValidateException;
use think\facade\Request;
use think\facade\Log;

class MemberController extends BaseController
{
    protected $memberDao;

    public function __construct(MemberDao $memberDao)
    {
        $this->memberDao = $memberDao;
    }

    /**
     * 获取项目成员列表
     * @param int $projectId 项目ID
     */
    public function members($projectId)
    {
        try {
            validate(MemberValidate::class)->scene('members')->check(['projectId' => $projectId]);

            $members = $this->memberDao->getMembersByProjectId($projectId);
            return $this->returnJson($members ?: []);

        } catch (ValidateException $e) {
            return $this->returnJson([], Code::UNPROCESSABLE_ENTITY, $e->getMessage());
        } catch (\Exception $e) {
            Log::error("获取项目成员失败: " . $e->getMessage());
            return $this->returnJson([], Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }

    /**
     * 审核成员信息变更
     * @param int $id 成员ID
     */
    public function verifyInfoChange($id)
    {
        try {
            validate(MemberValidate::class)->scene('verify')->check(['id' => $id]);

            $data = Request::put();
            $result = $this->memberDao->updateMember($id, $data);

            return $result
                ? $this->returnJson()
                : $this->returnJson([], Code::BAD_REQUEST, '信息变更审核失败');

        } catch (ValidateException $e) {
            return $this->returnJson([], Code::UNPROCESSABLE_ENTITY, $e->getMessage());
        } catch (\Exception $e) {
            Log::error("审核成员信息变更失败: " . $e->getMessage());
            return $this->returnJson([], Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }

    /**
     * 添加项目成员
     */
    public function addMember()
    {
        try {
            $data = Request::post();
            validate(MemberValidate::class)->scene('add')->check($data);

            $result = $this->memberDao->addMember(
                $data['user_id'],
                $data['project_id'],
                $data['class'] ?? '',
                $data['id_card'] ?? '',
                $data['address'] ?? '',
                $data['skilled_tech'] ?? ''
            );

            return $result
                ? $this->returnJson($result)
                : $this->returnJson([], Code::BAD_REQUEST, '添加成员失败');

        } catch (ValidateException $e) {
            return $this->returnJson([], Code::UNPROCESSABLE_ENTITY, $e->getMessage());
        } catch (\Exception $e) {
            Log::error("添加成员失败: " . $e->getMessage());
            return $this->returnJson([], Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }

    /**
     * 获取成员详细信息
     * @param int $userId 用户ID
     */
    public function getMemberInfo($userId)
    {
        try {
            validate(MemberValidate::class)->scene('user')->check(['userId' => $userId]);

            $member = $this->memberDao->getMemberByUserId($userId);
            return $member
                ? $this->returnJson($member)
                : $this->returnJson([], Code::NOT_FOUND, '成员不存在');

        } catch (ValidateException $e) {
            return $this->returnJson([], Code::UNPROCESSABLE_ENTITY, $e->getMessage());
        } catch (\Exception $e) {
            Log::error("获取成员信息失败: " . $e->getMessage());
            return $this->returnJson([], Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }
}